<?php
/**
* View a single page
*  Show normal pages.  Note that this file uses rewritten urls set in .htaccess
*  If the page hasn't been created yet, redirect via heads to /new
*
* @package folio
* @param string $user The user/community to which this page belongs.
* @param string $page The title of the page being shown.
*/
    define("context", "folio_page_view");

    require("../../../includes.php");
    require_once("../sidebar.php");

    require_once("../view/comment.php");
    require_once("../view/tags.php");
    require_once("../view/page.php");
    require_once("../view/artifact.php");
    require_once("../view/decoration.php");

    require_once("../model/user.php");
    require_once("../model/resource.php");
    require_once("../model/file.php");

    global $CFG;
    global $PAGE;
    global $FOLIO_CFG;

    // Load form helper class
    $form = new folio_BaseView();

    // Retrieve passed variables (page name & user name, as well as created).
    //     These variables will be used by the menu system to build links.  See lib.php for more details.
    //    Note that the included side menu depends upon these variable names.
    $_title = folio_page::title_decode(required_param('page'));
    $_username = folio_clean( required_param('user') );
    $url = url;

    // Load Page information
    $page = folio_page::SelectWhereTitleAndUsername( $_title, $_username );
    if( !$page ) {
        // Page hasn't been created yet.  Redirect to /new
        header("Location: {$url}{$_username}/page/{$_title}/new");
        exit();
    }

    // Don't use these vars, default to the ones in $page now.
    unset( $_username ); unset( $_title);

    // Load User Information
    $user = folio_user::SelectWhereIdent( $page->user_ident );
    if ( !$user )
        trigger_error("Invalid username $user->username passed to view.php", E_USER_ERROR);

    // set the current user.
    global $page_owner;
    $page_owner = $user->ident;


    // Test permissions.
    if( !$page->Permission('select') ) {

        // We don't have permissions, and so need some sort of title.
        $html_title = $page->title;
        $plain_title = $page->title;
        $body = __gettext('You do not have permission to view this page. Please contact the page\'s' .
                ' owner and ask for the security to be set to <b>Public</b> or <b>Moderated</b>. ' .
                '  You will be able to view this page once that has been done.  If this page belongs ' .
                ' to a community, you may also try to join the community.  Once you are a member, ' .
                ' you will be able to see and edit the page.');

    } else {
        // Existing page.with permissions

        // Load the Menu Options.
        // Edit & History
        
        $edit = $page->Permission('update');
        
        if( $edit ) 
            $PAGE->menu_sub[] = array( 'name' => 'folio:edit',
                'html' => a_hrefg( "{$url}{$user->username}/page/" . folio_page::title_encode($page->title). "/edit",
                __gettext("Edit")));
        $PAGE->menu_sub[] = array( 'name' => 'folio:history',
            'html' => a_hrefg( "{$url}{$user->username}/page/" . folio_page::title_encode($page->title). "/history",
            __gettext("History")));
        if( $edit ) 
            $PAGE->menu_sub[] = array( 'name' => 'folio:delete',
                'html' => a_hrefg( "{$url}{$user->username}/page/" . folio_page::title_encode($page->title). "/delete",
                __gettext("Delete")));

        // Add link for a new page if we have permission.
        if( folio_page::InsertPermission($user) ) {
            $PAGE->menu_sub[] = array( 'name' => 'folio:addpage',
                'html' => a_hrefg( "{$url}{$user->username}/page/" . folio_page::title_encode($page->title). "/addpage",
                __gettext("Add Page")));
        }

        // Build nice titles
        $plain_title = $page->title;
        $html_title = "<a href='{$url}activity/user/{$user->username}/rss/page+page_comment/all/1:" .
                folio_createhash( $_SESSION['userid'] ) .
                "'><img border=0 src='{$url}mod/folio/image/feed.png' /></a> " .
                folio_PageBreadcrumb::html($page);

        // Add RSS Links.
        global $metatags;
        $metatags.=
            '<link rel="alternate" type="application/rss+xml" title="RSS" ' .
                "href=\"{$url}{$user->username}/subscribe/rss/page+page_comment/\" />";

        // Build the body fields
        $form->Load( new folio_PageBodyView( $page ));
        $form->Load( new folio_PageAttachmentView( $page ));
        $form->Load( new folio_TagViewer($page));
        $form->Load( new folio_PageChildrenList( $page ));
        $form->Load( new folio_CommentViewer( $page ));
        $form->Load( new folio_CommentBox( $page ));
		
		// Create the log entry
		folio_history_pagelog($page, 'view' );
    }

    $body = $form->toString();

    // setup side menu
    //sidebar_remove( array('sidebar-profile','folio_tree'), true);

    sidebar_add(11, 'folio_tree',
        '<h2>Pages</h2><ul>' .
        folio_sidebar_wiki( $user, $page ) . // function adds <li> tags automatically
        '</ul>');

    // Transfer into template & write.
    templates_page_setup();

    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $html_title,
                    'body' => $body
                ));
    echo templates_page_draw(array(
                    $plain_title, $body
                ));

?>
